/********************************Copyright (c)**********************************\
**
**                   (c) Copyright 2025, Main, China, QD.
**                           All Rights Reserved
**
**                                 By(wo4fisher)
**                           http://www.wo4fisher.com
**
**----------------------------------文件信息------------------------------------
** 文件名称: buttonPort.c
** 创建人员: wht
** 创建日期: 2025-07-29
** 文档描述:
**
**----------------------------------版本信息------------------------------------
** 版本代号: V0.1
** 版本说明: 初始版本
**
**------------------------------------------------------------------------------
\********************************End of Head************************************/
#include "buttonPort.h"

volatile uint8_t g_button_code = BUTTON_NONE_E;

Button_t Button1;
//Button_t Button2;

uint8_t Read_Button1_Level ( void ) {
    return HAL_GPIO_ReadPin(BUTTON1_GPIO_Port,BUTTON1_Pin);
}

void Btn1_Down_CallBack ( void *btn )  {
    uint8_t msg = BUTTON_1_E;
    PRINT_INFO ( "Button1 click!" );
    //按键按下，发送消息队列
    g_button_code = BUTTON_1_E;
}
//void Btn1_Long_CallBack(void *btn)
//{
//}

void button_init ( void )
{
    /*------- 0. gpio init -----------*/
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    __HAL_RCC_GPIOB_CLK_ENABLE();

    GPIO_InitStruct.Pin = BUTTON1_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(BUTTON1_GPIO_Port, &GPIO_InitStruct);

    /*------- 1. create btn-----------*/
    Button_Create("Btn1",&Button1,Read_Button1_Level,GPIO_PIN_RESET); //加光耦，按下后高电平
//    Button_Create("Btn2",&Button2,Read_Button2_Level,GPIO_PIN_RESET);

    /*------- 2. button attach---------*/
    Button_Attach(&Button1,BUTTON_DOWN,Btn1_Down_CallBack);         //按键单击
//    Button_Attach(&Button2,BUTTON_DOWN,Btn2_Down_CallBack);         //按键单击

//    Button_Attach(&Button1,BUTTON_DOUBLE,Btn1_Double_CallBack);   //双击
//    Button_Attach(&Button1,BUTTON_LONG,Btn1_Long_CallBack);       //长按
}

uint8_t is_button_pressed()
{
    if(g_button_code == BUTTON_1_E)
    {
        g_button_code = BUTTON_NONE_E;
        return 1;
    }
    return 0;
}

void button_reset(void)
{
    g_button_code = BUTTON_NONE_E;
}

/********************************End of File************************************/
